import { View } from "@tarojs/components";
import "./app.scss";
import { getToken, loginOut } from "./utils/token";
import { getCurrentInstance } from "@tarojs/runtime";
import { useFamilyStore } from "./model";
import Taro from "@tarojs/taro";

const App = ({ children }) => {
  const { family } = useFamilyStore();
  const { router } = getCurrentInstance();
  const path = router?.path; //路由地址

  if (!path?.startsWith("/loginAndRegister") && !getToken()) {
    loginOut();
  }
  if (
    !path?.startsWith("/loginAndRegister") &&
    getToken() &&
    path !== "/innerPages/family/index" &&
    !family?.id
  ) {
    Taro.redirectTo({
      url: "/innerPages/family/index",
    });
  }

  return <View>{children}</View>;
};

export default App;
